﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZK.Common;
using ZK.InterfaceServer;

namespace ZK.YHP_TransManage
{
    public partial class Login : Form
    {
        private WFAutoSizeHelper wFAutoSizeHelper = new WFAutoSizeHelper();
        public Login()
        {
            InitializeComponent();
            Init();
            this.StartPosition = FormStartPosition.CenterScreen;
            wFAutoSizeHelper.GetAllInitInfo(this.Controls[0], this);
        }
        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            textBoxPassWord.UseSystemPasswordChar = true;

            this.MaximizeBox = false;//使最大化窗口失效
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;//下一句用来禁止对窗口大小进行拖拽
        }
        private readonly Models.LoginUserM loginUserM = new Models.LoginUserM();

        /// <summary>
        /// 登录按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender, EventArgs e)
        {
            loginUserM.username = textBoxUserName.Text.Trim();
            loginUserM.password = textBoxPassWord.Text.Trim();
            if (string.IsNullOrWhiteSpace(loginUserM.username))
            {
                MessageDialog.MessageBoxShow(
                    Com.TipsStringHelper.yhmbnwk,
                    Common.Utility.GetDisplayName<Models.LoginUserM>(
                        nameof(Models.LoginUserM.username)
                        )
                    );
                return;
            }
            if (string.IsNullOrWhiteSpace(loginUserM.password))
            {
                MessageDialog.MessageBoxShow(
                    Com.TipsStringHelper.mmbnwk,
                    Common.Utility.GetDisplayName<Models.LoginUserM>(
                        nameof(Models.LoginUserM.password)
                        ));
                return;
            }
            Models.ReceiveDataBase<Models.LoginReceiveM> receiveDataBase = LoginApiServer.Login(loginUserM);
            if (receiveDataBase == null || !receiveDataBase.success)
            {
                MessageDialog.MessageBoxShow("登录失败！", Com.TipsStringHelper.dlcw);
                return;
            }
            IConfig.GetSetuserInfo = new IConfig.UserInfo()
            {
                PassWord = loginUserM.password,
                LoginAccount = loginUserM.username,
                Token = receiveDataBase.data.deptList[0].token
            };

            this.Hide();
            Index index = new Index();
            index.Show();
        }

        private void Login_SizeChanged(object sender, EventArgs e)
        {
            if (wFAutoSizeHelper.ControlsInfo.Count > 0)//如果字典中有数据，即窗体改变
            {
                wFAutoSizeHelper.ControlsChangeInit(this.Controls[0]);//表示pannel控件
                wFAutoSizeHelper.ControlsChange(this.Controls[0]);

            }
        }
        
        private void Login_Load(object sender, EventArgs e)
        {
            var userInfo = IConfig.GetSetuserInfo;
            textBoxUserName.Text = userInfo.LoginAccount;
            textBoxPassWord.Text = userInfo.PassWord;
        }
    }
}
